<?php

use yii\helpers\Url;
use kartik\form\ActiveForm;
use kartik\detail\DetailView;
use admin\modules\buildingOrder\models\forms\BuildingOrders;
use admin\modules\buildingOrder\models\searches\BuildingOrderMessages;

/* @var $this yii\web\View */
/* @var $model BuildingOrders */
/* @var $lvMessageModel BuildingOrderMessages */
/* @var $form ActiveForm */

;$css = <<<CSS
.media>.media-body img,.media>.media-body video{
    max-width: 25% !important;
}
CSS;
$this->registerCss($css);

$leaveMessages = [];
$leaveMessageQuery = BuildingOrderMessages::find()
    ->andFilterWhere(['building_order_id' => $model->id])
    ->orderBy(['id' => SORT_DESC]);
$leaveMessages = $leaveMessageQuery->all();
foreach ($leaveMessages as $k => $v) {
    $leaveMessages[$k]->message = $v->getThumbedMessage();
    $leaveMessages[$k]->_set_bo_status = BuildingOrders::getStatusDesc()[$v->set_bo_status];
}

$userAvatar0 = THIS_SERVER_BASE_URI.Yii::getAlias('@static_url/images/no-avatar1.png');
$userAvatar0 = Yii::$app->urlManager->createAbsoluteUrl(['/pub/file/thumb', 'src' => $userAvatar0, 'maxWidth' => 50]);
$authAvatar1 = THIS_SERVER_BASE_URI.Yii::getAlias('@static_url/images/no-avatar1.png');
$authAvatar1 = Yii::$app->urlManager->createAbsoluteUrl(['/pub/file/thumb', 'src' => $authAvatar1, 'maxWidth' => 50]);
$csAvatar = THIS_SERVER_BASE_URI.Yii::getAlias('@static_url/images/defaultAvatar.jpg');
$csAvatar = Yii::$app->urlManager->createAbsoluteUrl(['/pub/file/thumb', 'src' => $csAvatar, 'maxWidth' => 50]);
$thumbPre = Yii::$app->urlManager->createAbsoluteUrl(['/pub/file/thumb']);
//dd($thumbPre);
?>
<div class="building-orders-test">
    <div class="row">
        <div class="col-sm-4">
            <?= DetailView::widget([
                'model' => $model,
                'condensed' => true,
                'hover' => true,
                'enableEditMode' => false,
                'labelColOptions' => ['style' => 'width: 30%'],
                'attributes' => [
                    'id',
                    'title',
                    [
                        'label' => '客户',
                        'attribute' => 'custom_name',
                        'format' => 'raw',
                    ],
                    [
                        'label' => '客户手机',
                        'attribute' => 'custom_mobile',
                        'format' => 'raw',
                    ],
                    [
                        'label' => '客户地区',
                        'value' => function () use ($model) {
                            return $model->county->merger_name;
                        },
                        'format' => 'raw',
                    ],
                    [
                        'label' => '创建员工',
                        'value' => function () use ($model) {
                            return $model->createdByAdmin->mobile;
                        },
                        'format' => 'raw',
                        'visible' => !!$model->createdByAdmin,
                    ],
                    [
                        'label' => '更新员工',
                        'value' => function () use ($model) {
                            return $model->updatedByAdmin->mobile;
                        },
                        'format' => 'raw',
                        'visible' => !!$model->updatedByAdmin,
                    ],
                    [
                        'label' => '客户地址',
                        'attribute' => 'address',
                        'format' => 'raw',
                    ],
                    [
                        'label' => '状态',
                        'value' => function () use ($model) {
                            return $model::getStatusDesc()[$model->status];
                        },
                        'format' => 'raw',
                    ],
                    'created_at:datetime',
                    'updated_at:datetime',
                ],
            ]) ?>
        </div>
        <div class="col-sm-8">
            <div class="panel panel-info" style="height: 600px;overflow-y: auto;">
                <div class="panel-heading">留言区</div>
                <div class="panel-body">
                    <?php foreach($leaveMessages as $k => $v): ?>
                        <?php if($v->created_by_admin && $v->created_by_admin === Yii::$app->user->id): ?>
                            <div class="media">
                                <div class="media-left">
                                    <a href="#">
                                        <img class="media-object img-rounded" src="<?=$authAvatar1 ?>">
                                    </a>
                                </div>
                                <div class="media-body">
                                    <h6 class="media-heading">[<?=$v->createdByAdmin->mobile ?> <?=$v->_set_bo_status ?>] <?=date("Y-m-d H:i", $v->created_at) ?></h6>
                                    <p><?=$v->message ?></p>
                                </div>
                            </div>
                        <?php else: ?>
                            <div class="media">
                                <div class="media-body">
                                    <h6 class="media-heading">[<?=$v->createdByAdmin->mobile ?> <?=$v->_set_bo_status ?>] <?=date("Y-m-d H:i", $v->created_at) ?></h6>
                                    <p><?=$v->message ?></p>
                                </div>
                                <div class="media-right">
                                    <a href="#">
                                        <?php if($v->created_by_admin): ?>
                                            <img class="media-object img-rounded" src="<?=$csAvatar ?>">
                                        <?php else: ?>
                                            <img class="media-object img-rounded" src="<?=$userAvatar0 ?>">
                                        <?php endif; ?>
                                    </a>
                                </div>
                            </div>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <div class="col-sm-12">
            <div><?= $this->render("/lv-message/_form", ['model' => $lvMessageModel]) ?></div>
        </div>
    </div>
</div>

<?php
$JS = <<<JS
$(".media-body img").click(function () {
    let imgUrl = $(this).attr('src')
    let thumbPre = "<?=$thumbPre ?>"
    thumbPre = "?src=" + thumbPre;
    imgUrl.replace(thumbPre, '', imgUrl)
    window.open(imgUrl, '_blank')
});
JS;
$this->registerJs($JS);
?>